package com.zdsoft.blp.zongbuju.wode.activity;

import android.content.Intent;
import android.graphics.Paint;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.application.MyApp;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.zongbuju.shouye.taoshihui.activity.ZhiFuActivity;
import com.zdsoft.blp.zongbuju.wode.entity.Order;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/5.
 */
public class WoDeDingDanXiangQingActivity extends BaseActivity implements NetCallback{
    private ImageView iv_show;
    private FanHuiView back;
    private TextView tv_name,tv_price,tv_dingdanhao,tv_time,
            tv_shuliang,tv_zongjia,tv_oldPrice,tv_zhifu,tv_phone,tv_zhifufangshi,tv_peisongfangshi;
    private static final  int ORDERINFO =1;
    private String goodsId = "";
    @Override
    protected void init() {
        setContentView(R.layout.wd_dingdanxiangqing);
        back=find(R.id.back);
        back.getTv_title().setText("订单详情");

        iv_show=find(R.id.iv_show);
        tv_name=find(R.id.tv_name);
        tv_price=find(R.id.tv_price);
        tv_dingdanhao=find(R.id.tv_dingdanhao);
        tv_time=find(R.id.tv_time);
        tv_shuliang=find(R.id.tv_shuliang);
        tv_zongjia=find(R.id.tv_zongjia);
        tv_oldPrice=find(R.id.tv_oldPrice);
        tv_zhifu=find(R.id.tv_zhifu);
        tv_phone=find(R.id.tv_phone);
        tv_zhifufangshi=find(R.id.tv_zhifufangshi);
        tv_peisongfangshi=find(R.id.tv_peisongfangshi);
        getorderInfo();
    }

    public void getorderInfo() {
        Net net = new Net();
        net.url = API.GOODSORDER_URL;
        net.nameSpace = API.GOODSORDER_NAMESPACE;
        net.methodName = API.ORDERINFO;
        Map<String, Object> map = new HashMap<>();
        map.put("id", getIntent().getStringExtra("orderId")+"");
        net.parms = map;
        net.code = ORDERINFO;
        getServer(net, WoDeDingDanXiangQingActivity.this);
    }
    @Override
    public void onResponse(boolean flag, int code, String res) {
        if(flag&&code==ORDERINFO){
            try {

                JSONObject object = new JSONObject(res);
                JSONArray array = object.getJSONArray("orderGoodsDetailForms");
                for(int i =0;i<array.length();i++){
                    JSONObject goods = array.getJSONObject(i);
                    tv_name.setText(goods.getString("goodsName"));
                    MyApp.setImage(goods.getString("picPath"),iv_show);
                    tv_price.setText(goods.getDouble("discountPrice")+"");
                    tv_oldPrice.setText(goods.getDouble("oldPrice")+"元");
                    tv_oldPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                    goodsId = goods.getString("goodsId");
                }
                if(object.getBoolean("canEvaluation")){
                    tv_zhifu.setText("立即评价");
                }else if(object.getBoolean("canPayment")){
                    tv_zhifu.setText("立即支付");
                    tv_zhifu.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(WoDeDingDanXiangQingActivity.this, ZhiFuActivity.class);
                            intent.putExtra("goodsName",tv_name.getText().toString());
                            intent.putExtra("price",tv_price.getText().toString());
                            intent.putExtra("number",tv_shuliang.getText().toString());
                            intent.putExtra("zPrice",tv_zongjia.getText().toString());
                            intent.putExtra("goodsId",goodsId);
                            WoDeDingDanXiangQingActivity.this.startActivity(intent);
                        }
                    });
                }
                tv_dingdanhao.setText(object.getString("id"));
                tv_time.setText(object.getString("createOrderTime"));
                tv_phone.setText("");
                tv_shuliang.setText(object.getInt("goodsCount")+"");
                tv_zongjia.setText(object.getDouble("money")+"");
                tv_zhifufangshi.setText(object.getString("payWay"));
                tv_peisongfangshi.setText(object.getString("psfs"));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }
}
